package com.newstart.springdatajpa.project.controller;

import com.newstart.springdatajpa.common.api.ApiResult;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "redis")
public class RedisController {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * set值
     * @return
     */
    @RequestMapping(value = "setValue", method = RequestMethod.GET)
    public ApiResult setValue() {
        //获取到operations用来操作redis数据
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        operations.set("name1","菜菜");

        //批量保存
        Map<String,Object> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        operations.multiSet(map);

        operations.append("key1","追加数据,存在就追加,不存在就保存");

        Boolean aBoolean = operations.setIfAbsent("delay", "存在就返回false并且不set值,不存在就返回true且将值set入redis");
        if(aBoolean){
            operations.set("delay","设置过期时间10分钟",10, TimeUnit.MINUTES);
        }

        return ApiResult.success(aBoolean);
    }

    /**
     * get值
     * @return
     */
    @RequestMapping(value = "getValue", method = RequestMethod.GET)
    public ApiResult getValue() {
        //获取到operations用来操作redis数据
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        Object o = operations.get("name1");

        //批量get
        List<String> list = new ArrayList<>();
        list.add("key1");
        list.add("key2");
        list.add("key3");
        List<Object> objects = operations.multiGet(list);

        return ApiResult.success(objects);
    }

    /**
     * delete值
     * @return
     */
    @RequestMapping(value = "deleteValue", method = RequestMethod.GET)
    public ApiResult deleteValue() {
    redisTemplate.delete("name1");
        return ApiResult.success();
    }

    /**
     * increase值
     * @return
     */
    @RequestMapping(value = "incrValue", method = RequestMethod.GET)
    public ApiResult incrValue() {
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        Long age = operations.increment("age");//自增,原子性给某个key+1,如果key不存在,直接设为1

        //每次自增3
        Long num = operations.increment("num", 3);

        Long count = operations.decrement("count", 1);//自减,原子性给某个key-1,如果key不存在,直接设为-1

        return ApiResult.success(age+"-"+num+"-"+count);
    }

    /**
     * haset的put值
     */
    @GetMapping("hashPut")
    public ApiResult hashPut(){
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        map1.put("name","苏东坡");
        map1.put("age",18);
        map2.put("name","刘禹锡");
        map2.put("age",20);

        operations.put("user","1",map1);
        operations.put("user","2",map2);
        return ApiResult.success();
    }
    /**http://localhost:8080/dev/redis/hashGet
     * haset的get值
     */
    @GetMapping("hashGet")
    public ApiResult hashGet(){
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        //判断key是否存在
        if(operations.hasKey("user", "1")){
            System.out.println("根据大key和小key查询"+operations.get("user", "1"));
            System.out.println("获取所有的key:"+operations.keys("user"));
            System.out.println("获取所有的value："+operations.values("user"));
            Map<Object, Object> map = operations.entries("user");
            for(Map.Entry<Object,Object> entry : map.entrySet()){
                System.out.println(entry.getKey()+" "+entry.getValue());
            }

        }
        return ApiResult.success();
    }

    /**
     * list的set值
     */
    @GetMapping("listSet")
    public ApiResult listSet(){
        ListOperations<String, Object> operations = redisTemplate.opsForList();
        operations.rightPush("names","张三");
        operations.rightPush("names","李四");
        operations.rightPushAll("names","王五","赵六");
        return ApiResult.success();
    }
    /**
     * list的get值
     */
    @GetMapping("listGet")
    public ApiResult listGet(){
        ListOperations<String, Object> operations = redisTemplate.opsForList();
        System.out.println("根据下标值从左边开始获取元素："+operations.index("names",0));
        System.out.println("获取索引之间数据:"+operations.range("names",0,-1));
        return ApiResult.success();
    }
    /**
     * list的remove值
     */
    @GetMapping("listRemove")
    public ApiResult listRemove(){
        ListOperations<String, Object> operations = redisTemplate.opsForList();
        System.out.println("删除左边元素："+operations.leftPop("names"));
        System.out.println("删除右边元素:"+operations.rightPop("names"));
        return ApiResult.success();
    }
    /**
     * set的set值
     */
    @GetMapping("setSet")
    public ApiResult setSet(){
        SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();
        opsForSet.add("names1","张三","lisi","wangwu","zhaoliu");
        opsForSet.add("names2","张三","lisi","tianqi","wangba");
        System.out.println("查询所有："+opsForSet.members("names1"));
        return ApiResult.success();
    }






}
